package com.djsearch.widget;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import android.util.Log;

public class HttpDownloader {
 
	private URL url = null;
	//private String path = "lijingdemo2/";
	private String path = "xzt/readerclient/html/";


	/**
	 * 閺嶈宓乁RL娑撳娴囬弬鍥︽,閸撳秵褰侀弰顖濈箹娑擃亝鏋冩禒璺虹秼娑擃厾娈戦崘鍛啇閺勵垱鏋冮張锟介崙鑺ユ殶閻ㄥ嫯绻戦崶鐐诧拷鐏忚鲸妲搁弬鍥ㄦ拱瑜版挷鑵戦惃鍕敶鐎癸拷1.閸掓稑缂撴稉锟介嚋URL鐎电钖�
	 * 2.闁俺绻僓RL鐎电钖�閸掓稑缂撴稉锟介嚋HttpURLConnection鐎电钖�3.瀵版鍩孖nputStream 4.娴犲丢nputStream瑜版挷鑵戠拠璇插絿閺佺増宓�
	 * 
	 * @param urlStr
	 * @return
	 */
	public String download(String urlStr) {
		StringBuffer sb = new StringBuffer();
		String line = null;
		BufferedReader buffer = null;
		try {
			url = new URL(urlStr);
			HttpURLConnection urlConn = (HttpURLConnection) url
					.openConnection();
			buffer = new BufferedReader(new InputStreamReader(
					urlConn.getInputStream()));
			while ((line = buffer.readLine()) != null) {
				sb.append(line);
			}

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				buffer.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return sb.toString();
	}

	/**
	 * 
	 * @param urlStr
	 * @param path
	 * @param fileName
	 * @return -1:閺傚洣娆㈡稉瀣祰閸戞椽鏁�0:閺傚洣娆㈡稉瀣祰閹存劕濮�1:閺傚洣娆㈠鑼病鐎涙ê婀�
	 */
	public int downFile(String urlStr) {
		String fileName = urlStr.substring(urlStr.lastIndexOf("=") + 1,
				urlStr.length())
				+ ".html";
		InputStream inputStream = null;
		try {
			FileUtils fileUtils = new FileUtils();
			if (fileUtils.checkSd() == true) {
				if (fileUtils.isFileExist(path + fileName)) {
					return 1;
				} else {
					inputStream = getInputStreamFromURL(urlStr);
					File resultFile = null;
					if(inputStream!=null){
						 resultFile = fileUtils.write2SDFromInput(path,
								fileName, inputStream);
					}else{
						return -1;
					}
					if (resultFile == null) {
						return -1;
					}
				}
			}else{

				return -1;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return -1;
		} finally {
			try {
				if (inputStream != null) {
					inputStream.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return 0;
	}

	/**
	 * 閺嶈宓乁RL瀵版鍩屾潏鎾冲弳濞达拷
	 * 
	 * @param urlStr
	 * @return
	 */
	public InputStream getInputStreamFromURL(String urlStr) {
		HttpURLConnection urlConn = null;
		InputStream inputStream = null;
		try {
			url = new URL(urlStr);
			urlConn = (HttpURLConnection) url.openConnection();
			inputStream = urlConn.getInputStream();

		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

		return inputStream;
	}
	
	
	public String getPath() {
		return path;
	}
	public void setPath(String path) {
		this.path = path;
	}
}
